package com.example.toutiao.ui.main.presenter

import com.example.toutiao.http.HttpUtils
import com.example.toutiao.http.ResponseListener
import com.example.toutiao.http.UserApi
import com.example.toutiao.mvp.model.BaseModel
import com.example.toutiao.mvp.presenter.BasePresenter
import com.example.toutiao.ui.main.model.CodeModel
import com.example.toutiao.ui.main.model.MainModel
import com.example.toutiao.ui.main.model.RegisterModel
import com.example.toutiao.ui.main.view.RegisterView

class RegisterPresenter: BasePresenter<RegisterView>() {
    /**
     * 获取验证码
     */
    fun getCode(phoneNumber:String){
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).getCode(phoneNumber),object :
            ResponseListener<BaseModel<CodeModel>> {
            override fun onSuccess(data: BaseModel<CodeModel>) {
                if(data!=null){
                    getBaseView()!!.setData(data.data)
                }
            }

            override fun onFail(err: String) {
                getBaseView()!!.setError(err)
            }

        })
    }

    /**
     * 账号注册
     */
    fun getRegister(phone:String,code:String,type:Int){
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).getRegister(phone,code,1,type),object :
            ResponseListener<BaseModel<RegisterModel>> {
            override fun onSuccess(data: BaseModel<RegisterModel>) {
                if(data!=null){
                    getBaseView()!!.onRegister(data.data)
                }
            }

            override fun onFail(err: String) {
                getBaseView()!!.setError(err)
            }

        })
    }

    fun setPassword(phone:String,password:String){
        HttpUtils.sendHttp(HttpUtils.createApi(UserApi::class.java).setPassword(phone,password),object :
            ResponseListener<BaseModel<RegisterModel>> {
            override fun onSuccess(data: BaseModel<RegisterModel>) {
                if(data!=null){
                    getBaseView()!!.setData(data.data)
                }
            }

            override fun onFail(err: String) {
                getBaseView()!!.setError(err)
            }

        })
    }
}